package com.wirelesshomecontrol.gui.windows;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.vaadin.data.util.QueryContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.wirelesshomecontrol.db.jdbcconnect;
import com.wirelesshomecontrol.gui.WirelessHomeControlWindow;

public class SettingsWindow extends Window{
	
	Window settingsWindow = new Window();
	private jdbcconnect cn = new jdbcconnect();
	 private Button btn_clr_login = new Button("Clear LOGIN History");
	 private Button btn_clr_sensor = new Button("Clear SENSOR History");
	 private Button btn_add_user = new Button("Add New User");
	 private Button btn_del_user = new Button("Delete User");
	 
	 private TextField txt_login = new TextField("Username");
		private TextField txt_password = new TextField("Password");
		
		public Table tblDataTable = null;

	public SettingsWindow() {
		initSettingsWindow();
	}

	public void initSettingsWindow() {
		settingsWindow.setCaption("Settings");
		settingsWindow.center();
		settingsWindow.setWidth("30%");
		settingsWindow.setHeight("85%");
		settingsWindow.center();
		
		txt_password.setSecret(true);
		txt_login.setRequired(true);
		txt_login.setRequiredError("You must enter a username");
		
		HorizontalLayout toolbar = new HorizontalLayout();
		HorizontalLayout toolbar2 = new HorizontalLayout();
		
		settingsWindow.addComponent(new Label("Add/Remove User Access to System"));
		
		settingsWindow.addComponent(txt_login);
		settingsWindow.addComponent(txt_password);
		settingsWindow.addComponent(new Label("<br />",Label.CONTENT_XHTML));
		settingsWindow.addComponent(toolbar);
		
		toolbar.addComponent(btn_add_user);
		toolbar.addComponent(btn_del_user);
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
			cn.qcSQLSensor = new QueryContainer("SELECT username FROM user_login", con);
			tblDataTable = new Table();
			tblDataTable.setWidth(90, Sizeable.UNITS_PERCENTAGE);
			tblDataTable.setContainerDataSource(cn.qcSQLSensor);
 
			tblDataTable.setVisible(true);
			tblDataTable.setEnabled(true);
			tblDataTable.setCaption("Authorized Users");
			settingsWindow.addComponent(tblDataTable);
			// cn.qcSQLHistory.close(); // Closes Query Can Not reopen

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		settingsWindow.addComponent(new Label("<br /> <br /> <center> WARNING!!!!!<br />"
			    + "Once Table Data is CLEARED, it can NOT BE UNDONE!</center> " + "<br /> Use this Buttons Below to CLEAR Table Data" , Label.CONTENT_XHTML));
		settingsWindow.addComponent(toolbar2);
		
		toolbar2.addComponent(btn_clr_login);
		
		settingsWindow.addComponent(new Label("<br />",Label.CONTENT_XHTML));
		
		toolbar2.addComponent(btn_clr_sensor);
		
		btn_add_user.setIcon(new ThemeResource("icons/32/Add-male-user-32.png"));
		btn_del_user.setIcon(new ThemeResource("icons/32/Remove-male-user-32.png"));
        btn_clr_login.setIcon(new ThemeResource("icons/32/clean-32.png"));
        btn_clr_sensor.setIcon(new ThemeResource("icons/32/clean-32.png"));

		btn_clr_login.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try {
					Class.forName("com.mysql.jdbc.Driver");
					Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
					cn.qcSQLHistory = new QueryContainer("TRUNCATE TABLE log", con);
					
					//cn.qcSQLHistory.close(); // Closes Query Can Not reopen

				} catch (SQLException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (Exception e) {
				e.printStackTrace();
			}
				settingsWindow.showNotification("Login History Cleared");
			}
		});
		
		btn_clr_sensor.addListener(new Button.ClickListener() {
			
			
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try {
					Class.forName("com.mysql.jdbc.Driver");
					Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
					cn.qcSQLHistory = new QueryContainer("TRUNCATE TABLE sensor_history", con);
					
					// cn.qcSQLHistory.close(); // Closes Query Can Not reopen

				} catch (SQLException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}catch (Exception e) {
					e.printStackTrace();
				}
				settingsWindow.showNotification("Sensor History Cleared");
			}
		});
		
			btn_add_user.addListener(new Button.ClickListener() {
			
			
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				String UserIn = txt_login.getValue() + "";
				String login_add ="'" + txt_login.getValue() + "'";
				if (UserIn.equals("admin")){
					settingsWindow.showNotification("Admin already exists");
				}else{ 		
				
				String query = "Select username FROM user_login WHERE username=" + login_add;
				try {
					
					Class.forName("com.mysql.jdbc.Driver");
					Connection con = DriverManager.getConnection (cn.dbUrl,"root","Hippos44");
					PreparedStatement stmt = con.prepareStatement(query);
					
					int rs = stmt.executeUpdate(); //returns number rows affected
					if (rs ==0){
						settingsWindow.showNotification("User Already Exists");
					} else if ( rs > 0){
						
					}
					con.close();
					} //end try

					catch(ClassNotFoundException e) {
					e.printStackTrace();
					}

					catch(SQLException e) {
					e.printStackTrace();
					}
				
				 cn.Update_User_DB(txt_login.getValue(), txt_password.getValue());
				 
				settingsWindow.showNotification("User Added");
				}
			} 
		});
			
			btn_del_user.addListener(new Button.ClickListener() {
				
				
				public void buttonClick(ClickEvent event) {
					// TODO Auto-generated method stub
					String UserIn = txt_login.getValue() + "";
					if (UserIn.equals("admin")){
						settingsWindow.showNotification("Unable to Delete Admin");
					}else{
					String login_delete ="'" + txt_login.getValue() + "'";
					String query = "DELETE FROM user_login WHERE username=" + login_delete;
					try {
						
						Class.forName("com.mysql.jdbc.Driver");
						Connection con = DriverManager.getConnection (cn.dbUrl,"root","Hippos44");
						PreparedStatement stmt = con.prepareStatement(query);
						
						int rs = stmt.executeUpdate(); //returns number rows affected
						if (rs ==0){
							settingsWindow.showNotification("User Not Found");
						}else {
							settingsWindow.showNotification("User Removed");
						}
						con.close();
						} //end try

						catch(ClassNotFoundException e) {
						e.printStackTrace();
						}

						catch(SQLException e) {
						e.printStackTrace();
						}
					
				}
				}
			});
				
	}
	

	public Window getSettings() {
		return settingsWindow;
	}

	public void setSettingsWindow(Window settingsWindow) {
		this.settingsWindow = settingsWindow;
	}
	
}


